PROJECT := umu-launcher

# If this is changed to umu (uppercase), `uninstall` target will also remove the SLR directory
INSTALLDIR ?= umu

OBJDIR := builddir

PREFIX  ?= /usr
BINDIR  := $(PREFIX)/bin
LIBDIR  := $(PREFIX)/lib
DATADIR := $(PREFIX)/share
MANDIR  := $(DATADIR)/man

DESTDIR ?=
USERINSTALL ?= xfalse
FLATPAK ?= xfalse


.PHONY: all
ifeq ($(FLATPAK), xtrue)
all: version umu umu-launcher
else
all: version umu umu-docs umu-launcher
endif

.PHONY: install
ifeq ($(USERINSTALL), xtrue)
install: umu-install umu-launcher-install user-install
else
install: umu-install umu-launcher-install
endif

# Special case, do this inside the source directory for release distribution
umu/umu_version.json: umu/umu_version.json.in
	$(info :: Updating $(@) )
	cp $(<) $(<).tmp
	sed 's|##UMU_VERSION##|$(shell git describe --always --long --tags)|g' -i $(<).tmp
	mv $(<).tmp $(@)

.PHONY: version
version: umu/umu_version.json

version-install: version
	$(info :: Installing umu_version.json )
	install -d $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_version.json -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)


$(OBJDIR)/.build-umu: | $(OBJDIR)
	$(info :: Building umu )
	sed 's|##INSTALL_PATH##|$(DATADIR)/$(INSTALLDIR)|g' umu/umu-run.in > $(OBJDIR)/umu-run
	touch $(@)

.PHONY: umu
umu: $(OBJDIR)/.build-umu

umu-bin-install: umu
	install -d $(DESTDIR)$(BINDIR)
	install -Dm 755 $(OBJDIR)/$(<)-run $(DESTDIR)$(BINDIR)/umu-run


$(OBJDIR)/.build-umu-docs: | $(OBJDIR)
	$(info :: Building umu man pages )
	scdoc < docs/umu.1.scd > $(OBJDIR)/umu.1
	scdoc < docs/umu.5.scd > $(OBJDIR)/umu.5
	touch $(@)

.PHONY: umu-docs
umu-docs: $(OBJDIR)/.build-umu-docs

umu-docs-install: umu-docs
	$(info :: Installing umu man pages )
	install -d $(DESTDIR)$(MANDIR)/man1
	install -d $(DESTDIR)$(MANDIR)/man5
	install -m644 $(OBJDIR)/umu.1 $(DESTDIR)$(MANDIR)/man1/umu.1
	install -m644 $(OBJDIR)/umu.5 $(DESTDIR)$(MANDIR)/man5/umu.5


umu-dist-install:
	$(info :: Installing umu )
	install -d $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_consts.py    -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_proton.py    -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_log.py       -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_plugins.py   -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 755 umu/umu_run.py       -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_util.py      -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)
	install -Dm 644 umu/umu_runtime.py   -t $(DESTDIR)$(DATADIR)/$(INSTALLDIR)

ifeq ($(FLATPAK), xtrue)
umu-install: version-install umu-dist-install umu-bin-install
else
umu-install: version-install umu-dist-install umu-docs-install umu-bin-install
endif

ifeq ($(FLATPAK), xtrue)
UMU_LAUNCHER_COMMAND = org.openwinecomponents.umu.umu-launcher
else
UMU_LAUNCHER_COMMAND = $(DATADIR)/$(INSTALLDIR)/umu_run.py
endif

# umu-launcher is separate to allow control over installing the bin target
$(OBJDIR)/.build-umu-launcher: | $(OBJDIR)
	$(info :: Building umu-launcher )
	sed 's|##INSTALL_PATH##|$(UMU_LAUNCHER_COMMAND)|g' umu/umu-launcher/umu-run.in > $(OBJDIR)/umu-launcher-run
	touch $(@)

.PHONY: umu-launcher
umu-launcher: $(OBJDIR)/.build-umu-launcher

umu-launcher-bin-install: umu-launcher
	install -d $(DESTDIR)$(DATADIR)/steam/compatibilitytools.d/umu-launcher
	install -Dm 755 $(OBJDIR)/$(<)-run $(DESTDIR)$(DATADIR)/steam/compatibilitytools.d/umu-launcher/umu-run

umu-launcher-dist-install:
	$(info :: Installing umu-launcher )
	install -d $(DESTDIR)$(DATADIR)/steam/compatibilitytools.d/umu-launcher
	install -Dm 644 umu/umu-launcher/compatibilitytool.vdf -t $(DESTDIR)$(DATADIR)/steam/compatibilitytools.d/umu-launcher
	install -Dm 644 umu/umu-launcher/toolmanifest.vdf      -t $(DESTDIR)$(DATADIR)/steam/compatibilitytools.d/umu-launcher

umu-launcher-install: umu-launcher-dist-install umu-launcher-bin-install


$(OBJDIR):
	@mkdir -p $(@)


.PHONY: clean
clean:
	$(info :: Cleaning source directory )
	@rm -rf -v $(OBJDIR) umu/umu_version.json ./$(RELEASEDIR) $(RELEASEDIR).tar.gz


RELEASEDIR := $(PROJECT)-$(shell git describe --abbrev=0)
$(RELEASEDIR):
	mkdir -p $(@)

.PHONY: release
release: $(RELEASEDIR) | version
	$(info :: Creating source distribution for release )
	mkdir -p $(<)
	rm -rf umu/__pycache__
	cp -r umu packaging Makefile.in configure.sh README.md LICENSE $(<)
	tar -cvzf $(<).tar.gz $(<)


.PHONY: uninstall
# NEVER use a wildcard here
uninstall:
	$(info :: Removing $(INSTALLDIR) files in $(DESTDIR)$(BINDIR) )
	@rm -rf -v --preserve-root=all $(DESTDIR)$(BINDIR)/umu-run
	$(info :: Removing $(INSTALLDIR) directory in $(DESTDIR)$(DATADIR) )
	@rm -rf -v --preserve-root=all $(DESTDIR)$(DATADIR)/$(INSTALLDIR)


.PHONY: user-install
user-install:
	$(info :: --- )
	$(info :: Installed under user-only location "$(DATADIR)/$(INSTALLDIR)" )
	$(info :: To run you need to make sure "$(BINDIR)" is in your PATH )


# vim: ft=make
